<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
  <title>菜单列表</title>
  <div th:replace="common/link::header"></div>
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-card">
      <form class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
              <input type="text" name="keyword" placeholder="请输入名称" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <button type="button" class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
            <button type="reset" class="layui-btn layui-btn-primary" lay-submit lay-filter="reset">重置</button>
          </div>
        </div>
      </form>
      <div class="layui-card-body">
        <table id="menuTable" lay-filter="menuTable"></table>
        <script type="text/html" id="toolbar">
          <div class="layui-btn-container">
            <a class="layui-btn layui-btn-xs" lay-event="expand"><i class="layui-icon layui-icon-down"></i>全部展开</a>
            <a class="layui-btn layui-btn-xs" lay-event="fold"><i class="layui-icon layui-icon-up"></i>全部折叠</a>
            <a sec:authorize="hasAuthority('menu:add')" class="layui-btn layui-btn-xs" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加</a>
          </div>
        </script>
        <script type="text/html" id="visibleTpl">
          <input type="checkbox" lay-filter="visible" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" {{d.visible==='Y'?'checked':''}} />
        </script>
        <script type="text/html" id="tableBar">
          <a sec:authorize="hasAuthority('menu:add')" class="layui-btn layui-btn-xs" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加</a>
          <a sec:authorize="hasAuthority('menu:edit')" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
          <a sec:authorize="hasAuthority('menu:del')" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
      </div>
    </div>
  </div>
<div th:replace="common/script::footer"></div>
<script th:inline="javascript">
  layui.config({
      base: '/static/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index', //主入口模块
  }).use(['index', 'useradmin','treetable','table','crud'], function() {
      let $ = layui.$,
              form = layui.form,
              table = layui.table,
              crud = layui.crud,
              treetable = layui.treetable;
      //监听搜索
      form.on('submit(search)', function(data) {
        let keyword = data.field.keyword;
        crud.treeTableSearch('menuTable',keyword);
      });
      //监听重置
      form.on('submit(reset)', function(data) {
        Object.keys(data.field).forEach(key => (data.field[key] = ''));
        //执行重载
        initTable();
      });
      // 监听菜单状态
      form.on('switch(visible)', function (obj) {
        let id = obj.elem.value;
        let checked = obj.elem.checked ? 'Y' : 'N';
        active.changeMenuVisible(id,checked);
      });
      let initTable = function (data) {
          return treetable.render({
              treeColIndex: 1,          // 树形图标显示在第几列
              treeSpid: 0,            // 最上级的父级id
              treeIdName: 'id',       // id字段的名称
              treePidName: 'parentId',     // 父级节点字段
              treeDefaultClose: false,   // 是否默认折叠
              treeLinkage: true,        // 父级展开时是否自动展开所有子级
              elem: '#menuTable',
              toolbar: '#toolbar',
              height: 'full-110',
              url:  ctx + '/sysMenu/page',
              cellMinWidth: 80,
              where: data,
              cols: [[ //表头
                  {field: 'id', title: 'ID', hide:true},
                  {field: 'menuName', title: '菜单名称'},
                  {field: 'menuType', title: "菜单类型", templet: function (d) { return crud.getDictValue('menuType',d.menuType);}},
                  {field: 'url', title: '请求地址'},
                  {field: 'perms', title: '权限标识'},
                  {field: 'icon', title: '图标', templet: function (d) { return  `<i class="layui-icon ${d.icon}"></i>`;}},
                  {field: 'visible', align: "center", sort: true, templet: '#visibleTpl', title: '状态'},
                  {toolbar: '#tableBar', title: '操作', width: 300, align:'center',fixed: 'right'}
              ]]
          });
      };

      initTable();
      //头工具栏事件
      table.on('toolbar(menuTable)', function(obj) {
        let checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data;
        switch(obj.event){
          case 'expand':
            active.expand();
            break;
          case 'fold':
            active.fold();
            break;
          case 'add':
            active.addView();
            break;
        }
      });
      //监听行工具事件
      table.on('tool(menuTable)', function(obj) {
          let data = obj.data //获得当前行数据
              ,layEvent = obj.event;
          if(layEvent === 'edit'){
              active.editView(data.id);
          } else if(layEvent === 'remove'){
              active.remove(data.id);
          } else if(layEvent === 'add'){
            active.addView(data);
          }
      });

      /* 触发弹层 */
      let active = {
          /**
           * 展开所有
           */
          expand: function() {
              treetable.expandAll('#menuTable');
          },
          /**
           * 折叠所有
           */
          fold: function() {
              treetable.foldAll('#menuTable');
          },
          /**
           * 添加
           * */
          addView: function(data) {
              layer.open({
                   type: 2
                  ,shade: 0.3
                  ,title: "添加菜单"
                  ,content: fun.appendUrlParams(ctx + "/sysMenu/addPage", data, ["id","menuName"])
                  ,maxmin: true
                  ,scrollbar: false
                  ,area: ['100%', '100%']
                  ,btn: ['确定', '取消']
                  ,yes: function(index, layero){
                      let submit = layero.find('iframe').contents().find('#save-submit');
                      submit.trigger('click');
                  }
              });
          },
          /**
           * 编辑
           * @param id id
           * */
          editView: function(id) {
              layer.open({
                  type: 2
                  ,shade: 0.3
                  ,title: '修改菜单'
                  ,content: ctx + '/sysMenu/editPage/' + id
                  ,maxmin: true
                  ,scrollbar: false
                  ,area: ['100%', '100%']
                  ,btn: ['确定', '取消']
                  ,yes: function(index, layero){
                      let submit = layero.find('iframe').contents().find('#save-submit');
                      submit.trigger('click');
                  }
              });
          },
          /**
           * 删除菜单
           * @param id id
           */
          remove: function(id) {
              layer.confirm('真的删除行么', function(index){
                  //向服务端发送删除指令
                  $.ajax({
                      type: 'DELETE',
                      url:  ctx + '/sysMenu/remove/' + id,
                      contentType:'application/json;charset=UTF-8',
                      dataType: 'json',
                      success: function(result) {
                          layer.msg(result.message);
                          if (result.code === 200) {
                              layer.close(index);
                              initTable();
                          }
                      }
                  });
              });
          },
          /**
           * 修改菜单状态
           * @param id 菜单id
           * @param checked 状态
           */
          changeMenuVisible: function (id,checked) {
            $.ajax({
              type: 'POST',
              url:  ctx + '/sysMenu/edit',
              data: JSON.stringify({
                id: id,
                visible: checked
              }),
              contentType:'application/json;charset=UTF-8',
              dataType: 'json',
              success: function(result) {
                layer.msg(result.message);
                if (result.code === 200) {
                  initTable();
                }
              }
            });
          }
      }
  });
</script>
</body>
</html>
